package com.woniu.communitymanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.communitymanagement.entity.HttpResult;
import com.woniu.communitymanagement.entity.Parking;
import com.woniu.communitymanagement.service.ParkingService;
import com.woniu.communitymanagement.mapper.ParkingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author QIQI
* @description 针对表【parking(车位表)】的数据库操作Service实现
* @createDate 2022-11-30 09:43:56
*/
@Service
public class ParkingServiceImpl extends ServiceImpl<ParkingMapper, Parking>
    implements ParkingService{
    @Autowired(required = false)
    private ParkingMapper parkingMapper;
    @Override
    public HttpResult deleteById(Integer id) {
        HttpResult httpResult=null;
        boolean isDelete = removeById(id);
        if(isDelete){
            httpResult=new HttpResult(null,null,200,"删除成功");
        }else{
            httpResult=new HttpResult(null,null,500,"删除失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult updateParkingById(Parking parking) {
        HttpResult httpResult=null;
        boolean isUpdate = updateById(parking);
        if (isUpdate){
            httpResult=new HttpResult(null,null,200,"修改成功");
        }else{
            httpResult=new HttpResult(null,null,500,"修改失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectOne(Integer id) {
        HttpResult httpResult=null;
        Parking parking = parkingMapper.selectParkingOne(id);
        if(parking!=null){
            httpResult=new HttpResult(parking,1,200,"查询成功");
        }else{
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult insertOne(Parking parking) {
        HttpResult httpResult=null;
        int count = parkingMapper.insert(parking);
        if(count>0){
            httpResult=new HttpResult(null,null,200,"添加成功");
        }else{
            httpResult=new HttpResult(null,null,500,"添加失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult all(Integer id) {
        HttpResult httpResult=null;
        List<Parking> parkings=parkingMapper.all(id);
        if(parkings!=null&&parkings.size()>0){
            httpResult=new HttpResult(parkings,parkings.size(),200,"查询成功");
        }else{
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectParkingList(String numbers, Integer pageIndex, Integer pageSize) {
        HttpResult httpResult=null;
        List<Parking> parkings = parkingMapper.selectParkingList(numbers, (pageIndex - 1) * pageSize, pageSize);
        Integer count = parkingMapper.selectCount(numbers);
        if (parkings!=null && parkings.size()>0){
            httpResult=new HttpResult(parkings,count,200,"查询成功");
        }else {
            if(pageIndex>1){
                parkings = parkingMapper.selectParkingList(numbers, (pageIndex - 1) * pageSize, pageSize);
                httpResult=new HttpResult(parkings,count,200,"查询成功");
            }else{
                httpResult=new HttpResult(null,null,500,"查询失败");
            }
        }
        return httpResult;
    }
}




